package view;
import game.*;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import controller.ChessController;
/**
 * Creates the current view of the chess game for the player to begin selecting the game.
 * 
 * @author Nicole Waldrum
 * @date January 30, 2010
 * @version 1.0
 * 
 * Added the basic GUI so that the player can choose the game that they would like to implement.
 * 
 * @author Nicole Waldrum
 * @date February 19, 2010
 * @version 2.0
 *
 * @author Trevor Bastian
 * @date February 21, 2010
 * @version 2.0
 * 
 * @author Mark Kelly
 * @date February 21, 2010
 * @version 2.0
 *
 */
public class ChessGUI implements GameStateListener, OutcomeListener {
	private JFrame mainFrame;
	private JMenuBar menuBar;
	private JLabel boardPieces[];
	private ChessPanel cPanel;
	private ButtonPanel bPanel;
	private TimerPanel tPanel;
	private JTextArea textArea;
	private ChessController controller;

	/**
	 * 
	 */
	public ChessGUI() {		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		
		controller = new ChessController(this);
		
		initComponents();
	}
	
	/**
	 * initialize all GUI components
	 */
	private void initComponents() {
		mainFrame = new JFrame("Intellectual Thinking Game");
		mainFrame.setSize(500,580);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		menuBar = new JMenuBar();
		JMenu tempMenu;
		JMenuItem tempMenuItem;

		// File Menu
		tempMenu = new JMenu("File");
		
		tempMenuItem = new JMenuItem("Exit");
		tempMenuItem.addActionListener(controller);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
				
		// Game Type Menu
		tempMenu = new JMenu("Game Type");
		
		tempMenuItem = new JMenuItem("Chess");
		tempMenuItem.addActionListener(controller);
		tempMenu.add(tempMenuItem);
		
		tempMenuItem = new JMenuItem("Knight's Tour");
		tempMenuItem.addActionListener(controller);
		tempMenu.add(tempMenuItem);
		
		tempMenuItem = new JMenuItem("8 Queen's Problem");
		tempMenuItem.addActionListener(controller);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
		
		// Move Piece Menu
		tempMenu = new JMenu("Move Piece");
		
		tempMenuItem = new JMenuItem("Move");
		tempMenuItem.addActionListener(controller);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
		
		// Help Menu
		tempMenu = new JMenu("Help");
		
		tempMenuItem = new JMenuItem("Help");
		tempMenuItem.addActionListener(controller);
		tempMenu.add(tempMenuItem);
		
		menuBar.add(tempMenu);
		
		mainFrame.setJMenuBar(menuBar);
		
		// The text display area
		textArea = new JTextArea();
		
		Font font = new Font(Font.MONOSPACED, Font.PLAIN, 18);
		textArea.setFont(font);
		textArea.setEditable(false);
		
		mainFrame.add(textArea);
		
		mainFrame.setVisible(true);
	}
	
	/**
	 * Begins the implementation of the chess game.
	 * @param args
	 */
	public static void main (String[] args) {
		ChessGUI gui = new ChessGUI();
	}

	private void redraw () {
		textArea.setText(this.controller.boardString());
	}

	@Override
	public void pieceMoved(Piece p) {
		redraw();
	}
	
	public void showHelp() {
		JOptionPane.showMessageDialog(null, "Please only enter a letter and a number to move.", "How to Move Pieces", JOptionPane.OK_OPTION);
	}

	@Override
	public void gameWon(Team team) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void gameStarted() {
		redraw();
	}
}
